package com.maisha.utils;

import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class JSONUtils {
	private static ObjectMapper mapper = new ObjectMapper();
	
	/** 将list 转化成 json
	 * @param list
	 * @param w
	 */
	public static void list2Json(List<?> list,StringWriter w ){
		try {
			mapper.writeValue(w,list);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/*** 将map 转化成json
	 * @param map
	 * @param w
	 * @throws JsonGenerationException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public static void map2Json(Map<String,Object> map,StringWriter w) throws JsonGenerationException, JsonMappingException, IOException{
		mapper.writeValue(w,map);
	}
}
